home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp95 / freyja13.exe / lha / KEY.C < prev    next >
C/C++ Source or Header  |  1992-03-22  |  6KB  |  371 lines

  1. /* KEY.C -- Key Input Routines
  2.  
  3.     Written March 1991 by Craig A. Finseth
  4.     Copyright 1991 by Craig A. Finseth
  5. */
  6.  
  7. #include "freyja.h"
  8.  
  9. #define ZCY        '\031'
  10.  
  11. static struct buffer *buf_buf = NULL;
  12. static struct mark *buf_pt = NULL;
  13.  
  14. static int mac_buf[MACROMAX] = { KEYNONE };
  15. static int *mac_ptr = mac_buf;
  16. static FLAG mac_record = FALSE;
  17. static int mac_arg = KEYNONE;
  18.  
  19. static char *str_ptr = NULL;
  20. static int str_len = 0;
  21.  
  22. /* ------------------------------------------------------------ */
  23.  
  24. /* Ask a yes/no question in the echo line.  Return KEYQUIT, KEYABORT,
  25. 'Y', or 'N'. */
  26.  
  27. int
  28. KAsk(msg)
  29.     char *msg;
  30.     {
  31.     DEcho(msg);
  32.     for (;;) {
  33.         switch (xtoupper(KGetChar())) {
  34.  
  35.         case KEYQUIT:
  36.             DModeLine();
  37.             return(KEYQUIT);
  38.             /*break;*/
  39.  
  40.         case KEYABORT:
  41.         case ESC:
  42.         case BEL:
  43.             DModeLine();
  44.             return(KEYABORT);
  45.             /*break;*/
  46.  
  47.         case SP:
  48.         case 'Y':
  49.             DModeLine();
  50.             return('Y');
  51.             /*break;*/
  52.  
  53.         case DEL:
  54.         case BS:
  55.         case 'N':
  56.             DModeLine();
  57.             return('N');
  58.             /*break;*/
  59.  
  60.         default:
  61.             TBell();
  62.             break;
  63.             }
  64.         }
  65.     }
  66.  
  67.  
  68. /* ------------------------------------------------------------ */
  69.  
  70. /* begin defining keyboard macro */
  71.  
  72. void
  73. KBegMac()
  74.     {
  75.     uarg = 0;
  76.     if (mac_arg > 0) {
  77.         DError("Using macro");
  78.         return;
  79.         }
  80.     mac_ptr = mac_buf;
  81.     mac_record = TRUE;
  82.     }
  83.  
  84.  
  85. /* ------------------------------------------------------------ */
  86.  
  87. /* Echo msg if no char is typed within interval return true if msg
  88. printed, else false */
  89.  
  90. FLAG
  91. KDelayPrompt(msg)
  92.     char *msg;
  93.     {
  94.     int cnt;
  95.  
  96.     for (cnt = 0; cnt < DELAYCOUNT; cnt++) {
  97.         if (KIsKey() == 'Y') return(FALSE);
  98.         }
  99.     DEchoNM(msg);
  100.     return(TRUE);
  101.     }
  102.  
  103.  
  104. /* ------------------------------------------------------------ */
  105.  
  106. /* finish defining keyboard macro */
  107.  
  108. void
  109. KEndMac()
  110.     {
  111.     uarg = 0;
  112.     mac_record = FALSE;
  113.     mac_ptr -= 2;
  114.     if (mac_ptr < mac_buf) mac_ptr = mac_buf;
  115.     *mac_ptr = KEYNONE;
  116.     }
  117.  
  118.  
  119. /* ------------------------------------------------------------ */
  120.  
  121. /* Switch input to be from the specified buffer.  The entire buffer is
  122. read, starting from the beginning.  The point is preserved. */
  123.  
  124. void
  125. KFromBuf(bptr)
  126.     struct buffer *bptr;
  127.     {
  128.     struct buffer *savebuf = cbuf;
  129.  
  130.     BBufGoto(bptr);
  131.     if ((buf_pt = BMarkCreate()) != NULL) {
  132.         buf_buf = bptr;
  133.         BMoveToStart();
  134.         }
  135.     BBufGoto(savebuf);
  136.     }
  137.  
  138.  
  139. /* ------------------------------------------------------------ */
  140.  
  141. /* do keyboard macro */
  142.  
  143. void
  144. KFromMac()
  145.     {
  146.     if (mac_record) {
  147.         DError("Creating macro");
  148.         uarg = 0;
  149.         return;
  150.         }
  151.     mac_arg = uarg;
  152.     mac_ptr = mac_buf;
  153.     uarg = 0;
  154.     }
  155.  
  156.  
  157. /* ------------------------------------------------------------ */
  158.  
  159. /* Switch input to be from the supplied string. */
  160.  
  161. void
  162. KFromStr(str, len)
  163.     char *str;
  164.     int len;
  165.     {
  166.     if (str == NULL || len <= 0) {
  167.         str_len = 0;
  168.         }
  169.     else    {
  170.         str_ptr = str;
  171.         str_len = len;
  172.         }
  173.     }
  174.  
  175.  
  176. /* ------------------------------------------------------------ */
  177.  
  178. /* Get a character and handle keyboard macros */
  179.  
  180. int
  181. KGetChar()
  182.     {
  183.     int chr;
  184.     
  185.     if (buf_buf != NULL) {
  186.         struct buffer *savebuf = cbuf;
  187.  
  188.         BBufGoto(buf_buf);
  189.         if (!BIsEnd()) {    /* get next char */
  190.             chr = BGetCharAdv();
  191.             BBufGoto(savebuf);
  192.             return(chr);
  193.             }
  194.         BPointToMark(buf_pt);
  195.         BMarkDelete(buf_pt);
  196.         buf_buf = NULL;
  197.         BBufGoto(savebuf);
  198.         }
  199.     if (str_len > 0) {
  200.         str_len--;
  201.         return(*str_ptr++);
  202.         }
  203.     while (mac_arg > 0) {
  204.         chr = *mac_ptr++;
  205.         if (chr != KEYNONE) return(chr);
  206.         mac_arg--;
  207.         mac_ptr = mac_buf;
  208.         DIncrDisplay();
  209.         TForce();
  210.         }
  211.     TForce();
  212. #if defined(MSDOS)
  213.     if (c.g.key_type == 'J') {
  214.         chr = JGetKey();
  215.         }
  216.     else
  217. #endif
  218.         chr = TGetKey();
  219.  
  220.     if (c.g.ESC_swap != ESC) {
  221.         if (chr == ESC) chr = c.g.ESC_swap;
  222.         else if (chr == c.g.ESC_swap) chr = ESC;
  223.         }
  224.     if (mac_record) {
  225.         *mac_ptr++ = chr;
  226.         if (mac_ptr > &mac_buf[MACROMAX - 1]) {
  227.             DError("Keyboard macro full");
  228.             mac_ptr--;
  229.             }
  230.         *mac_ptr = KEYNONE;
  231.         }
  232.     return(chr);
  233.     }
  234.  
  235.  
  236. /* ------------------------------------------------------------ */
  237.  
  238. /* Input a string argument. Return KEYQUIT, KEYABORT, or 'Y' (if ok). */
  239.  
  240. int
  241. KGetStr(msg, str, len)
  242.     char *msg;
  243.     char *str;
  244.     int len;
  245.     {
  246.     char sbuf[BIGBUFFSIZE];
  247.     char buf[BIGBUFFSIZE];
  248.     int c;
  249.     int amt;
  250.     int retval;
  251.  
  252.     *sbuf = NUL;
  253.     for (retval = KEYNONE; retval == KEYNONE; ) {
  254.         xsprintf(buf, "%s: %s", msg, sbuf);
  255.         DEcho(buf);
  256.  
  257.         amt = strlen(sbuf);
  258.         c = KGetChar();
  259.         switch (c) {
  260.  
  261.         case KEYQUIT:
  262.             retval = KEYQUIT;
  263.             break;
  264.  
  265.         case KEYABORT:
  266.         case ESC:
  267.         case BEL:
  268.             retval = KEYABORT;
  269.             break;
  270.  
  271.         case CR:
  272.             retval = 'Y';
  273.             break;
  274.  
  275.         case BS:
  276.         case DEL:
  277.             if (*sbuf != NUL) sbuf[amt - 1] = NUL;
  278.             break;
  279.  
  280. #if defined(MSDOS)
  281.         case 0x100 + 97:    /* Ctrl-F4 */
  282. #endif
  283.         case ZCY:
  284.             KFromBuf(kill_buf);
  285.             break;
  286.  
  287.         default:
  288.             if (amt >= len - 1) {
  289.                 sbuf[amt - 1] = NUL;
  290.                 TBell();
  291.                 }
  292.  
  293.             if (c == ZCQ) c = KGetChar();
  294.             sbuf[amt] = c;
  295.             sbuf[amt + 1] = NUL;
  296.             break;
  297.             }
  298.         }
  299.     if (retval != KEYQUIT && retval != KEYABORT && *sbuf != NUL)
  300.         xstrcpy(str, sbuf);
  301.     DModeLine();
  302.     return(retval);
  303.     }
  304.  
  305.  
  306. /* ------------------------------------------------------------ */
  307.  
  308. /* Is key available from macro? */
  309.  
  310. char
  311. KIsKey()
  312.     {
  313.     if (buf_buf != NULL) {
  314.         struct buffer *savebuf = cbuf;
  315.  
  316.         BBufGoto(buf_buf);
  317.         if (!BIsEnd()) {
  318.             BBufGoto(savebuf);
  319.             return('Y');
  320.             }
  321.         BPointToMark(buf_pt);
  322.         BMarkDelete(buf_pt);
  323.         buf_buf = NULL;
  324.         BBufGoto(savebuf);
  325.         }
  326.     if (str_len > 0) return('Y');
  327.     if (!mac_record && mac_arg > 0) return('Y');
  328. #if defined(MSDOS)
  329.     if (c.g.key_type == 'J')
  330.         return(JIsKey());
  331.     else
  332. #endif
  333.         return(TIsKey());
  334.     }
  335.  
  336.  
  337. /* ------------------------------------------------------------ */
  338.  
  339. /* Return a pointer to the start of the keyboard macro. */
  340.  
  341. int *
  342. KMacPtr()
  343.     {
  344.     return(mac_buf);
  345.     }
  346.  
  347.  
  348. /* ------------------------------------------------------------ */
  349.  
  350. /* Internal routine to display current argument */
  351.  
  352. FLAG
  353. KUArg(targ)
  354.     int targ;
  355.     {
  356.     int cnt;
  357.     char buf[LINEBUFFSIZE];
  358.  
  359.     if (targ == 4) {
  360.         for (cnt = 0; cnt < DELAYCOUNT; cnt++) {
  361.             if (KIsKey() == 'Y') return(FALSE);
  362.             }
  363.         }
  364.     xsprintf(buf, "Arg: %d", targ);
  365.     DEchoNM(buf);
  366.     return(TRUE);
  367.     }
  368.  
  369.  
  370. /* KEY.C -- Key Input Routines */
  371.